package com.ruoyi.ysg.service.impl;

import java.util.Collections;
import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ysg.annotation.AddTeacherId;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ysg.mapper.YsgStudentMapper;
import com.ruoyi.ysg.domain.YsgStudent;
import com.ruoyi.ysg.service.IYsgStudentService;
import com.ruoyi.ysg.service.IYsgSetMealService;
import com.ruoyi.ysg.domain.YsgSetMeal;
import com.ruoyi.ysg.service.IYsgOrderService;
import com.ruoyi.ysg.domain.YsgOrder;

/**
 * 学生管理Service业务层处理
 *
 * @author xsz
 * @date 2025-09-24
 */
@Service
public class YsgStudentServiceImpl implements IYsgStudentService
{
    @Autowired
    private YsgStudentMapper ysgStudentMapper;

    @Autowired
    private IYsgOrderService ysgOrderService;

    @Autowired
    private IYsgSetMealService ysgSetMealService;


    /**
     * 查询学生管理
     *
     * @param id 学生管理主键
     * @return 学生管理
     */
    @Override
    public YsgStudent selectYsgStudentById(Long id)
    {
        return ysgStudentMapper.selectYsgStudentById(id);
    }

    /**
     * 查询学生管理列表
     *
     * @param ysgStudent 学生管理
     * @return 学生管理
     */
    @Override
    @AddTeacherId
    public List<YsgStudent> selectYsgStudentList(YsgStudent ysgStudent)
    {
        return ysgStudentMapper.selectYsgStudentList(ysgStudent);
    }

    /**
     * 新增学生管理
     *
     * @param ysgStudent 学生管理
     * @return 结果
     */
    @Override
    public int insertYsgStudent(YsgStudent ysgStudent)
    {
        ysgStudent.setCreateTime(DateUtils.getNowDate());
        return ysgStudentMapper.insertYsgStudent(ysgStudent);
    }

    /**
     * 修改学生管理
     *
     * @param ysgStudent 学生管理
     * @return 结果
     */
    @Override
    public int updateYsgStudent(YsgStudent ysgStudent)
    {
        ysgStudent.setUpdateTime(DateUtils.getNowDate());
        return ysgStudentMapper.updateYsgStudent(ysgStudent);
    }

    /**
     * 批量删除学生管理
     *
     * @param ids 需要删除的学生管理主键
     * @return 结果
     */
    @Override
    public int deleteYsgStudentByIds(Long[] ids)
    {
        return ysgStudentMapper.deleteYsgStudentByIds(ids);
    }

    /**
     * 删除学生管理信息
     *
     * @param id 学生管理主键
     * @return 结果
     */
    @Override
    public int deleteYsgStudentById(Long id)
    {
        return ysgStudentMapper.deleteYsgStudentById(id);
    }

    public int extend(YsgStudent ysgStudent, YsgSetMeal ysgSetMeal) {
        YsgStudent student = ysgStudentMapper.selectYsgStudentById(ysgStudent.getId());
        Long beforeHours = ObjectUtils.isNotEmpty(student.getClassHour()) ? student.getClassHour() : 0;

        if(ObjectUtils.isNotEmpty(ysgStudent.getClassHour()) && ysgStudent.getClassHour() > 0) {
            Long afterHours = beforeHours + ysgStudent.getClassHour();
            // 创建订单记录
            if(ObjectUtils.isNotEmpty(ysgSetMeal)) {
                YsgOrder order = new YsgOrder();
                order.setStudentId(ysgStudent.getId());
                order.setSetMealId(ysgSetMeal.getId());
                order.setAmount(ysgSetMeal.getTotal());
                order.setPurchaseHours(ysgStudent.getClassHour());
                order.setBeforeHours(beforeHours);
                order.setAfterHours(afterHours);
                order.setStatus(Constants.YES); // 已支付
                order.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
                order.setCreateTime(DateUtils.getNowDate());
                ysgOrderService.insertYsgOrder(order);
            }
            ysgStudent.setClassHour(afterHours);
            return ysgStudentMapper.updateYsgStudent(ysgStudent);
        }
        return 1;
    }
}
